package demo01;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;

import org.apache.commons.io.FileUtils;



public class zuoye02 {
	 private static boolean loginfalse = true;
	public static void main(String[] args) throws Exception,IOException{
//		FileUtils.copyFile(new File("E:\\1437970061\\user.txt"),new File("user.txt"));
		dongjie();
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入用户名=密码");
		String usr=sc.nextLine();
		sc.close();
		if(!loginfalse)
		{
			System.out.println("输入错误次数达到上限，您的账户已被冻结。");
			return;
		}
		int lineCount = 0;
		String line0=null;
		BufferedWriter bw = new BufferedWriter(new FileWriter("testusr", true));
		bw.write(usr+"\n");
		bw.close();
		
		BufferedReader br0=new BufferedReader(new FileReader("testusr"));
		while((line0=br0.readLine())!=null){
			lineCount++;
		}
		BufferedReader br=new BufferedReader(new FileReader("user.txt"));
	
		String userget = null;
		boolean find = true;
		while((userget=br.readLine())!=null)
		{
			 if(usr.split("=")[0].equals(userget.split("=")[0]))
				{
					if(usr.split("=")[1].equals(userget.split("=")[1]))
				 	{System.out.println("登录成功");
					FileWriter delete=new FileWriter("testusr");
					delete.write("");
					return;}
					else if(lineCount>=3 && lineCount<6)
					{
						 System.out.println("多次输入错误，请十秒后再重新输入");
		                    Thread.sleep(10000);
		                    System.out.println("请重新运行，输入用户名和密码");
		                    return;
					    }else if(lineCount>=6)
					    {
					    	System.out.println("输入错误次数达到上限，您的账户已被冻结。");
					    	loginfalse=false;
					    	BufferedWriter wr=new BufferedWriter(new FileWriter("stop.txt"));
							wr.write(String.valueOf(loginfalse));
							wr.close();
							return;	 
					    } 
					    else{
			    	System.out.println("密码错误");
				    	return;
					    }
				}
		}
	
			System.out.println("用户不存在");
		
	}

	private static void dongjie() throws IOException {
		BufferedReader rd=new BufferedReader(new FileReader("stop.txt"));
		 String line=rd.readLine();
		 if (line != null) {
             loginfalse = Boolean.parseBoolean(line.trim());
         } else {
             loginfalse = true; 
         }
	}
	}